import math

def validpeak_new(sample_sign, s2limit, wavelength, dspacing, term1, term2, term3, const):

    pi = 3.14159265
    
    #multfactor is the multiplier for the FWHM which will be used to determine the scan range
    
    multfactor = 1.0
    
    if wavelength < 2*dspacing:

        tth=2*180.0*math.asin(wavelength/(2*dspacing))/3.14159265
        
        widthsq = (term1 - (term2*math.tan(tth*pi/360))+(term3*math.tan(tth*pi/360)*math.tan(tth*pi/360)))/const
        width=math.sqrt(widthsq)

        tthmax = tth+(multfactor*width)
        tthmin = tth-(multfactor*width)
        tthmin=round(tthmin,2)
        tthmax=round(tthmax,2)
        
        s2comp=abs(s2limit)
        
        if tthmax > s2comp:
            isvalid=0
        else:
            tthmin=sample_sign*tthmin
            tthmax=sample_sign*tthmax
            isvalid=1
    else:
        tthmin=-1.00
        tthmax=-1.00
        isvalid=0
    return isvalid, tthmin, tthmax